package main

import (
	"fmt"
	"math/rand"
	"os"
	"time"
)

func main() {
	// Check if questions file exists
	_, err := os.Stat("data/24point_questions.txt")
	if os.IsNotExist(err) {
		fmt.Println("Questions file not found. Generating questions...")
		err := generateAndSaveQuestions()
		if err != nil {
			fmt.Printf("Error generating questions: %v\n", err)
			return
		}
	} else if err != nil {
		fmt.Printf("Error checking questions file: %v\n", err)
		return
	}

	// Load questions
	questions, err := loadQuestions()
	if err != nil {
		fmt.Printf("Error loading questions: %v\n", err)
		return
	}

	// Check if we have questions
	if len(questions) == 0 {
		fmt.Println("暂无题目，请重新运行")
		return
	}

	// Seed the random number generator
	rand.Seed(time.Now().UnixNano())

	// Select 100 random questions (or all if less than 100)
	numQuestions := len(questions)
	selectedCount := 100
	if numQuestions < 100 {
		selectedCount = numQuestions
	}

	// Randomly select and display questions
	indices := rand.Perm(numQuestions)[:selectedCount]

	for i, idx := range indices {
		question := questions[idx]
		fmt.Printf("%d.     %5d%5d%5d%5d\n", 
			i+1, question[0], question[1], question[2], question[3])
	}
}